/*
 * Created on Oct 18, 2003
 *
 */
package schoolsource.model;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.logging.Level;

import schoolsource.sql.SchoolDBSQL;
import schoolsource.util.SchoolLogger;

/**
 *
 * @author Steven Steele
 * Copyright 2003
 *
 * Contains information for contacts (i.e. the associations between sender, receiver,
 * and the message sent).
 */
public class ContactContainer extends Container {

	// The map of timer buckets for all databases
	protected static Hashtable dbStores = new Hashtable();
	// The auto-generated id of the message for which this is a contact.
	private IntegerItem messageAssignedID =
		new IntegerItem(SchoolDBSQL.MSGASSIGNEDID, "", true);
	// The auto-generated id of the intended recipient.
	private StringItem toID = new StringItem(SchoolDBSQL.CTTOID, "", true);
	// The auto-generated id of the sender.
	private StringItem fromID = new StringItem(SchoolDBSQL.CTFROMID, "", true);
	// The auto-generated id fo the class to which this contact is related.
	private StringItem classAssignedID =
		new StringItem(SchoolDBSQL.CLASSASSIGNEDID, "", true);
	// Indicates whether or not the message has been read by the intended recipient.
	private BooleanItem hidden =
		new BooleanItem(SchoolDBSQL.CTHIDDEN, "false", true);
	// A reference to the Message Container to which this contact refers.
	private MessageContainer message = null;// = new MessageContainer();

	/**
	 * Default constructor.
	 */
	private ContactContainer() {
	}

	/**
	 * Constructor that stored the passed-in MessageContainer in the container.
	 * @param mc A container of the message to which this contact refers.
	 */
	private ContactContainer(MessageContainer mc) {
		message = mc;
		messageAssignedID = mc.getAssignedMessageID();
	}

	/**
	 * Constructor that loads information from the database to populate the container.
	 * @param newMsgAssignedID The message to which the contact relates.
	 * @param toID The auto-generated ID of the intended recipient of the message.
	 * @param fromID The auto-generated ID of the sender of the message.
	 */
	private ContactContainer(String newMsgAssignedID, String toID, String fromID, String dbName) {
		getToID().setValue(toID);
		getFromID().setValue(fromID);
		SchoolDBSQL.getSchoolDBSQL(dbName).loadContact(
			this,
			new Item[] {getToID(), new IntegerItem(SchoolDBSQL.MSGASSIGNEDID, newMsgAssignedID, true), getFromID()});
		messageAssignedID = getMessage().getAssignedMessageID();
	}

	public static ContactContainer getContactContainer(String newMsgAssignedID, String toID, String fromID, String dbName) {
		ContactContainer returnVal = null;
		DBStore dbs = null;

		if ((dbs = (DBStore) dbStores.get(dbName)) == null) {
			dbs = new DBStore();
			dbStores.put(dbName, dbs);
			dbs.start();
		}

		if ((returnVal = (ContactContainer) dbs.getContainer("M" + newMsgAssignedID + "T" + toID + "F" + fromID)) == null) {
			returnVal = new ContactContainer();
			returnVal.getMessageAssignedID().setValue(newMsgAssignedID);
			returnVal.getToID().setValue(toID);
			returnVal.getFromID().setValue(fromID);
			SchoolDBSQL.getSchoolDBSQL(dbName).loadContact(returnVal, new Item[] {returnVal.getMessageAssignedID(),
																				  returnVal.getFromID(),
																				  returnVal.getToID()});
			returnVal.message = MessageContainer.getMessageContainer(newMsgAssignedID, dbName);
			dbs.addContainer(returnVal, "M" + newMsgAssignedID + "T" + toID + "F" + fromID);
		}

		return returnVal;
	}

	public static ContactContainer getContactContainer(String classAssignedID, String toID, String fromID, MessageContainer mc, String dbName) {
		ContactContainer returnVal = new ContactContainer(mc);
		returnVal.getClassAssignedID().setValue(classAssignedID);
		returnVal.getToID().setValue(toID);
		returnVal.getFromID().setValue(fromID);
//		returnVal.message = MessageContainer.getMessageContainer("-1", dbName);
returnVal.message = mc;
		return returnVal;
	}

	/* (non-Javadoc)
	 * @see schoolsource.model.Container#getValidationItems()
	 */
	public Item[] getValidationItems() {
		ArrayList al = new ArrayList();
		al.add(toID);
		al.add(fromID);
		al.add(hidden);
		al.add(classAssignedID);
		al.add(messageAssignedID);

		return (Item[]) al.toArray(new Item[al.size()]);
	}

	/**
	 * Getter for the message to which the contact refers.
	 * @return The message to which the contact refers.
	 */
	public MessageContainer getMessage() {
		return message;
	}

	/**
	 * Getter for the auto-generated ID of the message to which the contact refers.
	 * @return The auto-generated ID of the message to which the contact refers.
	 */
	public IntegerItem getMessageAssignedID() {
		return messageAssignedID;
	}

	/**
	 * Getter for the auto-generated ID of the class to which this contact is related.
	 * @return The auto-generated ID of the class to which this contact is related.
	 */
	public StringItem getClassAssignedID() {
		return classAssignedID;
	}

	/**
	 * Getter for the auto-generated ID of the sender of the message.
	 * @return The auto-generated ID of the sender of the message.
	 */
	public StringItem getFromID() {
		return fromID;
	}

	/**
	 * Getter for whether the message has been read by the intended recipient.
	 * @return Indicates true if the message has been read, false otherwise.
	 */
	public BooleanItem getHidden() {
		return hidden;
	}

	/**
	 * Getter for the auto-generated ID of the intended recipient of the message.
	 * @return The auto-generated ID of the intended recipient of the message.
	 */
	public StringItem getToID() {
		return toID;
	}

	/**
	 * Prints the values of the container.
	 */
	public void printItems() {
		SchoolLogger.logger.log(
			Level.INFO,
			"************************************************");
		Item iList[] = getValidationItems();
		for (int j = 0; j < iList.length; j++) {
			if (iList[j] != null) {
				SchoolLogger.logger.log(
					Level.INFO,
					iList[j].getFieldName() + ": " + iList[j].toString());
			}
		}
		getMessage().printItems();
	}
}
